package com.sysassist.taskmgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.sysassist.R;

import android.app.ActivityManager;
import android.app.ListActivity;
import android.app.ActivityManager.RunningTaskInfo;
//import android.content.Context;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class TaskMgrActivity extends ListActivity {
	/** RunningTaskInfo */
	private static List<RunningTaskInfo> taskList = null;
	/** maxTaskNum */
    private static final int maxTaskNum = 100;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_list);
        
        taskList = new ArrayList<RunningTaskInfo>();
        getTaskInfo();
        showTaskInfo();
    }
    
    /**
     * showTaskInfo
     */
    private void showTaskInfo() {
        List<HashMap<String,String>> infoList = new ArrayList<HashMap<String,String>>();
        for (Iterator<RunningTaskInfo> iterator = taskList.iterator(); iterator.hasNext();) {
            RunningTaskInfo taskInfo = iterator.next();
            HashMap<String, String> map = new HashMap<String, String>();
            String taskNameStr = taskInfo.baseActivity.toString();
            int startIndex = taskNameStr.indexOf("{");
            int endIndex = taskNameStr.indexOf("}");
            String taskName = taskNameStr.substring(startIndex + 1, endIndex);
            map.put("task_name", taskName);
            map.put("task_id", "Task ID:" + taskInfo.id);
            infoList.add(map);
        }
        
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this, 
                infoList, 
                R.layout.task_list_item, 
                new String[]{"task_name", "task_id"},
                new int[]{R.id.task_name, R.id.task_id} );
        setListAdapter(simpleAdapter);
    }
    
    /**
     * getTaskInfo
     * @return
     */
    private int getTaskInfo() {
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        taskList = activityManager.getRunningTasks(maxTaskNum);
        return taskList.size();
    }
    
    /*public static String getRunningTaskInfo(Context context) { 
    	StringBuffer sInfo= new StringBuffer();  
    	final ActivityManager activityManager= (ActivityManager) context
    			.getSystemService(Context. ACTIVITY_SERVICE);  
    	List<RunningTaskInfo> tasks= activityManager.getRunningTasks(maxTaskNum);  
    	Iterator<RunningTaskInfo> l= tasks.iterator();  
    	while (l.hasNext()) 
    	{  
    		RunningTaskInfo ti= (RunningTaskInfo) l.next();  
    		sInfo.append("id:").append(ti.id);  
    		sInfo.append("\nbaseActivity:").append(ti.baseActivity.flattenToString());  
    		sInfo.append("\nnumActivities:").append(ti.numActivities);  
    		sInfo.append("\nnumRunning:").append(ti.numRunning);  
    		sInfo.append("\ndescription:").append(ti.description);  
    		sInfo.append("\n\n"); } 
    	return sInfo.toString();
    	}*/
    
    /**
     * 获取系统的任务信息，需要用户权限：android.permission.GET_TASKS
     * 
     * ActivityManager.RunningTaskInfo {
     *     public ComponentName baseActivity    // 任务做为第一个活动的组件信息
     *     public CharSequence description        // 任务当前状态的描述
     *     public int id                        // 任务的ID
     *     public int numActivities                // 任务中所包含的活动的数目
     *     public int numRunning                // 任务中处于运行状态的活动数目
     *     public Bitmap thumbnail                // 任务当前状态的位图表示，目前为null
     *     public ComponentName topActivity        // 处于任务栈的栈顶的活动组件
     * }
     */
}
